package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.*;
import DAO.*;
import service.*;

/**
 * 商品管理Servlet，处理商品删除、增加、修改、新增等操作
 * @author Administrator
 *
 */
public class VetServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public VetServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//获取前台参数，根据参数选择对应的servlet方法进行业务处理
		String op = request.getParameter("op");
		
		if("select".equals(op)){//更新商品
			selectService(request,response);
		}
		else if("del".equals(op)){//删除商品
			deleteService(request,response);
		}
		
		
	}
	/**
	 * 	宠物信息
	 * @param response
	 */
	private void selectService(HttpServletRequest request,
			HttpServletResponse response) {
		
		  IPetDao dao = DaoFactory.getProductDao();
		  Map<String,Object> map = null;

		  try{
		  if(dao.queryPet(map)!=null){
			  request.getRequestDispatcher("兽医信息.jsp").forward(request, response);
		  }else{
			  response.sendRedirect("main1.jsp");
		  }
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	}
	
	/**
	 * 删除宠物
	 * @param request
	 * @param response
	 */
	private void deleteService(HttpServletRequest request,
			HttpServletResponse response) {
		
		//接收前台传递的待删除商品ID，根据id删除对应商品
		//delete from product where id ="id";
		int id =Integer.parseInt(request.getParameter("id"));
		Vetbean p = new Vetbean();
		p.setId(id);
		IVetDao dao = DaoFactory.getVetDao();
		try{
		if(dao.delete(p)){
			request.getRequestDispatcher("兽医信息.jsp").forward(request, response);
			
		}else{
			response.sendRedirect("兽医信息.jsp");
		}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	

	public void init() throws ServletException {
	}

}

